package com.youyicloud.service;

import com.youyicloud.entity.EquipmentInfo;
import com.youyicloud.entity.EquipmentPay;
import com.youyicloud.entity.EquipmentType;
import com.youyicloud.entity.PayInfo;
import com.youyicloud.repository.EquipmentPayRepository;
import com.youyicloud.repository.EquipmentRepository;
import com.youyicloud.repository.EquipmentTypeRepository;
import com.youyicloud.repository.PayRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EquipmentService {

    @Autowired
    private EquipmentRepository equipmentRepository;
    @Autowired
    private EquipmentPayRepository equipmentPayRepository;
    @Autowired
    private PayRepository payRepository;
    @Autowired
    private EquipmentTypeRepository equipmentTypeRepository;

    public List<HashMap> findByImeiMac(String imeiMac){
        List<HashMap> hashMaps = new ArrayList<HashMap>();
        List<EquipmentInfo> equipmentInfos = equipmentRepository.findByImeiMacLike("%"+imeiMac+"%");
        for (EquipmentInfo equipmentInfo : equipmentInfos){
            EquipmentPay equipmentPay = equipmentPayRepository.findByEquipmentId(equipmentInfo.getEquipmentId());
            PayInfo payInfo = payRepository.findOne(equipmentPay.getPayId());
            HashMap tempMap = new HashMap();
            tempMap.put("imeiMac",equipmentInfo.getImeiMac());
            tempMap.put("equipmentBrevityCode",payInfo.getEquipmentBrevityCode());
            hashMaps.add(tempMap);
        }
        return hashMaps;
    }

    public EquipmentInfo findById(Integer equipmentId){
        return equipmentRepository.findOne(equipmentId);
    }

    public Map findMapById(Integer equipmentId){
        Map resultMap = new HashMap();
        EquipmentInfo equipmentInfo = equipmentRepository.findOne(equipmentId);
        EquipmentPay equipmentPay = equipmentPayRepository.findByEquipmentId(equipmentId);
        EquipmentType equipmentType = equipmentTypeRepository.findOne(equipmentInfo.getEquipmentTypeId());
        PayInfo payInfo = payRepository.findOne(equipmentPay.getPayId());
        resultMap.put("equipmentTypeName",equipmentType.getEquipmentTypeName());
        resultMap.put("imeiMac",equipmentInfo.getImeiMac());
        resultMap.put("equipmentBrevityCode",payInfo.getEquipmentBrevityCode());
        return resultMap;
    }

    public EquipmentInfo saveEquipmentInfo(String imeiMac){
        EquipmentInfo equipmentInfo = new EquipmentInfo();
        equipmentInfo.setImeiMac(imeiMac);
        equipmentRepository.save(equipmentInfo);
        return equipmentInfo;
    }


}
